/*
package com.java.test.timer.horseracelamp;

import com.java.test.messagecenter.service.MessageCenterService;
import com.java.test.timer.HorseRaceLampPushTimer;
import com.java.test.util.RequestGameServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

*/
/**
 * ProducerCustomerModelUsingBlockedQueue
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/20 0020 10:47
 *//*

@Slf4j
public class ProducerCustomerModelUsingBlockedQueue {

    public static void push(HorseRaceLampDTO horseRaceLampDTO) {
       */
/* BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(horseRaceLampDTO.getRollNumber());
        horseRaceLampDTO.setQueue(queue);
        String time = new Date().getTime()+"";
        Random r1 = new Random();
        String url =  RequestGameServiceUtil.createUrl(time,r1.nextInt(100)+"",horseRaceLampDTO.getData(),horseRaceLampDTO.getOffLineUserUrl());
        JSONObject resMsg = RequestGameServiceUtil.getService(url);
        if(resMsg!=null){
            String state = (resMsg.get("state").toString());
            if("0".equals(state)){
                horseRaceLampDTO.getMessageCenterService().updateHorseRaceLampStatus(horseRaceLampDTO.getEntityId());
                //log.info(resMsg.get("message").toString());
            }else {
                log.info(resMsg.get("message").toString());
            }
        }*//*

        */
/*new Thread(new Producer(horseRaceLampDTO)).start();
        new Thread(new Customer(horseRaceLampDTO)).start();*//*

    }

    private static class Producer implements Runnable{
        private int MAX_SIZE;
        BlockingQueue<Integer> queue;
        Producer(){}
        Producer(HorseRaceLampDTO horseRaceLampDTO){
            MAX_SIZE = horseRaceLampDTO.getRollNumber();
            queue = horseRaceLampDTO.getQueue();
        }
        @Override
        public void run() {
            boolean flag = true;
            while(flag){
                try {
                    if(queue.size()==MAX_SIZE){
                        flag = false;
                    }else {
                        queue.put(1);
                        //log.info("创建"+queue.size());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class Customer implements Runnable{
        private String jsonData = "";
        private BlockingQueue<Integer> queue;
        private long sleep = 0;
        private String offLineUserUrl;
        private MessageCenterService messageCenterService;
        private long id;
        private Integer version;

        Customer(){ }
        Customer(HorseRaceLampDTO horseRaceLampDTO){
            jsonData =  horseRaceLampDTO.getData();
            queue = horseRaceLampDTO.getQueue();
            sleep = horseRaceLampDTO.getCustomerSleep();
            offLineUserUrl = horseRaceLampDTO.getOffLineUserUrl();
            messageCenterService = horseRaceLampDTO.getMessageCenterService();
            id=horseRaceLampDTO.getEntityId();
            version = horseRaceLampDTO.getVersion();
        }
        @Override
        public void run() {
            boolean flag = true;
            while(flag){
                //log.info("进行了消费"+sleep);
                try {
                    Integer edition = HorseRaceLampPushTimer.threadFlag.get(""+id);
                    if(edition!=version){
                        flag = false;
                    }else {
                        Integer sleepVal = HorseRaceLampPushTimer.sleepVal.get(""+id);
                        if(sleepVal!=null&&!"".equals(sleepVal)){
                            continue;
                        }
                        if(queue.size()==0){
                            int i = messageCenterService.updateHorseRaceLampStatus(id);
                            if(i>0){
                                flag = false;
                            }else {
                                log.error("修改跑马灯状态出现了异常！");
                            }
                        }else {
                            int i = messageCenterService.updateHorseRaceLampStatus(id);
                            if(i>0){
                                String time = new Date().getTime()+"";
                                Random r1 = new Random();
                                String url =  RequestGameServiceUtil.createUrl(time,r1.nextInt(100)+"",jsonData,offLineUserUrl);
                                JSONObject resMsg = RequestGameServiceUtil.getService(url);
                                if(resMsg!=null){
                                    String state = (resMsg.get("state").toString());
                                    if("0".equals(state)){
                                        queue.take();
                                        //log.info("消费了"+queue.size());
                                        //log.info(resMsg.get("message").toString());
                                    }else {
                                        log.info(resMsg.get("message").toString());
                                    }
                                }
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
*/
